package com.csaba.swing.gui.login;

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import com.csaba.connector.model.Customer;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.util.MaxLengthDocument;
import com.csaba.swing.gui.wizard.WizardContext;
import com.csaba.swing.gui.wizard.WizardDialog;
import com.csaba.util.encryption.EncryptedStore;

@SuppressWarnings( "serial" )
public class DefaultLoginPage extends AbstractLoginPage
{
	private static final String LD_PASSWORD = "password"; //$NON-NLS-1$

	protected JTextField loginIdField;
	protected JPasswordField passwordField;
	private JCheckBox savePassword;

	@Override
	public void init( final WizardContext context )
	{
		super.init(context);

		add(new JLabel(MainWindow.getString("DefaultLoginPage.loginId"))); //$NON-NLS-1$
		add(loginIdField = new JTextField(new MaxLengthDocument(20), "", 20)); //$NON-NLS-1$

		add(new JLabel(MainWindow.getString("DefaultLoginPage.password"))); //$NON-NLS-1$
		add(passwordField = new JPasswordField(new MaxLengthDocument(20), "", 20)); //$NON-NLS-1$

		add(new JSeparator());
		add(savePassword = new JCheckBox(MainWindow.getString("DefaultLoginPage.savePassword"))); //$NON-NLS-1$

		addChangeListener(loginIdField);
		addChangeListener(passwordField);
	}

	@Override
	protected void updateMessage( final Component component )
	{
		super.updateMessage(component);

		final WizardDialog dialog = context.getDialog();
		final String loginId = loginIdField.getText();
		if ( loginId == null || loginId.length() < 1 )
		{
			dialog.setReady(false);
			dialog.setWarning(MainWindow.getString("DefaultLoginPage.msg.validLogin")); //$NON-NLS-1$
			return;
		}
		final char[] password = passwordField.getPassword();
		try
		{
			if ( password == null || password.length < 1 )
			{
				dialog.setReady(false);
				dialog.setWarning(MainWindow.getString("DefaultLoginPage.msg.validPassword")); //$NON-NLS-1$
				return;
			}
		}
		finally
		{
			EncryptedStore.clear(password);
		}

		dialog.clearMessage();
		dialog.setReady(true);
	}

	@Override
	public boolean leave( final int action )
	{
		if ( action == ACTION_FINISH )
		{
			final Customer customer = new Customer();
			customer.setLoginId(loginIdField.getText());
			customer.setPassword(new String(passwordField.getPassword()));

			return doLogin(customer);
		}
		return true;
	}

	@Override
	public void updateView()
	{
		super.updateView();

		savePassword.setEnabled(( (LoginWizardModel) context.getWizardModel() ).isSaveDetails());
		if ( !savePassword.isEnabled() )
			savePassword.setSelected(false);
	}

	@Override
	protected LoginDetails getDetailsToSave( final Customer customer )
	{
		final LoginDetails details = new LoginDetails(customer.getBank().getName(), customer.getLoginId());

		if ( savePassword.isSelected() )
			details.put(LD_PASSWORD, customer.getPassword());

		return details;
	}

	@Override
	protected void setSavedDetails( final LoginDetails details )
	{
		loginIdField.setText(details.getLoginId());
		final String password = details.get(LD_PASSWORD);
		passwordField.setText(password);
		savePassword.setSelected(password != null);
	}

}
